package com.somlsis.fragment

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.LinearLayoutManager
import com.base.BaseAdapter
import com.base.BaseFragment
import com.clj.fastble.BleManager
import com.clj.fastble.callback.BleScanCallback
import com.clj.fastble.data.BleDevice
import com.somlsis.R
import com.somlsis.activity.AddDeviceActivity
import com.somlsis.adapter.SearchDeviceAdapter
import com.somlsis.databinding.FragmentSearchDeviceBinding


class SearchDeviceFragment(val listener: AddDeviceActivity.OnAddDeviceNextClickListener) :
    BaseFragment<FragmentSearchDeviceBinding>() {
    private lateinit var deviceAdapter: SearchDeviceAdapter
    private  var anim:ObjectAnimator?=null
    override fun getViewBinding(): FragmentSearchDeviceBinding {
        return FragmentSearchDeviceBinding.inflate(layoutInflater)
    }

    override fun createView() {
        if (deviceCenter.bleManager.isBlueEnable) {
            /*蓝牙未打开*/

        }

        binding.phonePermissionSettings.setOnClickListener {
            //跳转应用消息，间接打开应用权限设置-效率高
            //跳转应用消息，间接打开应用权限设置-效率高
            val intent = Intent()
            intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            val uri = Uri.fromParts("package", context?.let { it.packageName }, null)
            intent.data = uri
            startActivity(intent)
        }
        binding.help.setOnClickListener {
            searchDevice()
        }
        searchDevice()
        context?.let {
            deviceAdapter = SearchDeviceAdapter(it, object :BaseAdapter.OnItemClickListener<BleDevice> {
                override fun onItemClick(position: Int, data: BleDevice?) {
                    data?.let {
                        listener.onSearchDeviceConnectClick(data)
                    }
                }
            })
        }
        binding.rvDevices.adapter = deviceAdapter
        binding.rvDevices.layoutManager = LinearLayoutManager(context)

        anim=getRotation(binding.ivDeviceSearch)
    }

    override fun refreshUI() {
    }

    private fun isBlueEnable() {

    }

    private fun searchDevice() {
        BleManager.getInstance().scan(object : BleScanCallback() {
            override fun onScanStarted(success: Boolean) {
                anim?.let {
                    if (!it.isRunning){
                        it.start()
                    }else if (it.isPaused){
                        it.resume()
                    }
                }

            }

            override fun onLeScan(bleDevice: BleDevice) {

            }

            override fun onScanning(bleDevice: BleDevice) {
                deviceAdapter.addData(bleDevice)
            }

            override fun onScanFinished(scanResultList: List<BleDevice>) {
                anim?.let {
                    it.pause()
                }


            }
        })
    }
    private fun getRotation(view: View): ObjectAnimator {
        val animator = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f)
        animator.duration = 3600
        animator.interpolator = LinearInterpolator()
        animator.repeatMode = ValueAnimator.RESTART
        animator.repeatCount = -1
        return animator
    }

    override fun onDestroy() {
        super.onDestroy()
        anim?.let {
            it.cancel()
        }

    }
}